import { useConfigStore } from '@renderer/store/useConfigStore'
import { ElMessage } from 'element-plus'
import { storeToRefs } from 'pinia'
import router from '@renderer/router'

export default () => {
  const { imgUrl, saveDirectory } = storeToRefs(useConfigStore())

  const setWallpaper = async () => {
    const state = await window.api.checkDirectory(saveDirectory.value)
    if (!state) {
      ElMessage({ type: 'error', message: '图片保存目录无效' })
      router.push({ name: 'setting' })
    } else {
      window.api.setWallpaper(imgUrl.value, saveDirectory.value)
    }
  }

  // 下载图片
  const downloadImage = () => {
    window.api.downloadImage(imgUrl.value)
  }

  // 设置图片壁纸保存目录
  const setImageSaveDirectory = async () => {
    const path = await window.api.setImageSaveDirectory()
    if (path) {
      saveDirectory.value = path
    }
  }

  return { setWallpaper, downloadImage, setImageSaveDirectory }
}
